<?php

// Get variables
$client_id = get_input('client_id');
$userid = get_input('user_id');
if(!$userid)
    $userid = get_loggedin_userid();
$brief = get_input('brief');
$relationships = get_input('relationships');

$followEntities = array();

if($relationships) {
    $relationships = json_decode($relationships, TRUE);
    foreach($relationships as $relationship) {
        if($relationship == 'to') {
            $followers = get_whiteboard_follower_ids($userid);
            foreach ($followers as $follower) {
                $followEntities[] = $follower->user_id;
            }
        }
        if($relationship == 'from') {       
            $followings = get_whiteboard_following_ids($userid);
            foreach ($followings as $following) {
                $followEntities[] = $following->follow_id;
            }
        }
    }
} else {
    $followers = get_whiteboard_follower_ids($userid);
    foreach ($followers as $follower) {
        $followEntities[] = $follower->user_id;
    }    
    $followings = get_whiteboard_following_ids($userid);
    foreach ($followings as $following) {
        $followEntities[] = $following->follow_id;
    }
}

$follows = array();

foreach($followEntities as $followEntity) {
        $follow = get_user($followEntity);
	$follow = userToArray($follow, $client_id, $brief);
	$follows[] = $follow;
}

echo json_encode(export_result(200, $follows));

?>